package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import com.changgou.order.service.OrderService;
import com.changgou.util.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName CartController
 * @Description 商品存入Redis
 * @Author 洛尘
 * @Date 22:56 2019/11/23
 * @Version 2.1
 **/
@RestController
@RequestMapping("/cart")
@CrossOrigin
public class CartController {

    @Autowired(required = false)
    private CartService cartService;

    /**
     * 添加购物车
     * @username        用户名
     * @param skuId    商品详情id
     * @param num      商品数量
     * @return
     */
    @GetMapping("/add")
    public Result add(Long skuId, Integer num) {
        String username = TokenDecode.getUserInfo().get("username");
        cartService.add(username, skuId, num);
        return new Result(true, StatusCode.OK, "商品添加购物车成功");
    }

    @GetMapping("/list")
    public Result<List<OrderItem>> list() {
        String username = TokenDecode.getUserInfo().get("username");
        List<OrderItem> list = cartService.list(username);
        return new Result<>(true, StatusCode.OK, "商品查询成功",list);

    }
}
